#!/bin/bash

##############################################################################
#
# Library:   TubeTK
#
# Copyright 2010 Kitware Inc. 28 Corporate Drive,
# Clifton Park, NY, 12065, USA.
#
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################

##############################################################################
# This file is automatically generated during the TubeTK configuration process.
##############################################################################

# Check to make certain this file was called using "source <scriptname>" so that
# changes to PATH will be carried to the parent process
if [ "$0" = "$BASH_SOURCE" ]; then
  echo "ERROR: Please run this script by typing"
  echo "   source Setup_TubeTK-Experiments.sh"
  exit 1
fi

# Check to make certain that the scripts aren't already in the path.
if hash TubeTKTubeExtractor.sh 2>/dev/null; then
  echo "TubeTKTubeExtractor.sh already in path.  Environment is already setup."
  return 1
fi

# Use VesselView and SegmentTubes executables to find VesselView and TubeTK
export SegmentTubesCMD=SegmentTubes
export VesselViewCMD=VesselView
if [ "$(uname)" == "Darwin" ]; then
  echo "Running on Mac"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
  echo "Running on Linux"
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
  echo "Running on Windows"
  export SegmentTubesCMD=SegmentTubes.exe
  export VesselViewCMD=VesselView.exe
fi

# Find the path to TubeTK executables
if ! hash ${SegmentTubesCMD} 2>/dev/null; then
  if ! hash ${VesselViewCMD} 2>/dev/null; then
    echo "SegmentTubes and VesselView not found."
    echo "   Please install VesselView or TubeTK from http://tubetk.org"
    echo "   and make certain VesselView or TubeTK-build/bin is in your system path."
    return 1
  fi
  export VesselViewPath=`hash -t ${VesselViewCMD} | sed -e 's/\/${VesselViewCMD}$$//'`
  export TubeTKPath="${VesselViewPath}/Extensions/Applications"
  if ! [ -e ${TubeTKPath}/${SegmentTubesCMD} ]; then
    export TubeTKPath="${VesselViewPath}/lib/Slicer-4.3/cli-modules"
  fi
else
  echo "Found SegmentTubes"
  export TubeTKPath=`hash -t ${SegmentTubesCMD} | sed -e 's/\/SegmentTubes.*//'`
fi
echo "Found TubeTK executables at"
echo "   ${TubeTKPath}"

# Find the path to this script
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
export TubeTKExamplesPath=${DIR}
echo "Found TubeTK-Examples at"
echo "   ${TubeTKExamplesPath}"

# Set path to include TubeTK, if it isn't already in the path
if ! hash ${SegmentTubesCMD} 2>/dev/null; then
  echo "Adding path to TubeTK executables to shell's path"
  export PATH="${TubeTKPath}:${PATH}"
fi

# Set path to include TubeTK-Examples scripts
echo "Adding path to TubeTK-Examples' scripts to shell's path"
export PATH="${TubeTKExamplesPath}/BrainExtraction:${TubeTKExamplesPath}/TubeEnhancement:${TubeTKExamplesPath}/TubeExtraction:${TubeTKExamplesPath}/UltrasoundAngiography/MergeAdjacentImages:${TubeTKExamplesPath}/UltrasoundAngiography/VesselExtraction:${TubeTKExamplesPath}/LiverExtraction:${PATH}"

return 0
